package org.jeecg.modules.oa.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.oa.entity.OaReportWeekRead;
import org.jeecg.modules.oa.mapper.OaReportWeekReadMapper;
import org.jeecg.modules.oa.service.IOaReportWeekReadService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: 阅读信息表
 * @Author: jeecg-boot
 * @Date:   2019-09-16
 * @Version: V1.0
 */
@Service
public class OaReportWeekReadServiceImpl extends ServiceImpl<OaReportWeekReadMapper, OaReportWeekRead> implements IOaReportWeekReadService {
    @Autowired
    private OaReportWeekReadMapper oaReportWeekReadMapper;


    @Override
    public void updateReadInfo(String id, boolean isPC, SysUser user) {
        OaReportWeekRead info = oaReportWeekReadMapper.getReadInfo(id, user.getId());
        if (info == null) {
            info = new OaReportWeekRead();
        }
        if (isPC) {
            if (info.getPcFirstReadTime() == null) {
                info.setPcFirstReadIp(SecurityUtils.getSubject().getSession().getHost());
                info.setPcFirstReadTime(new Date());
            } else {
                info.setPcLastReadIp(SecurityUtils.getSubject().getSession().getHost());
                info.setPcLastReadTime(new Date());
            }
        } else {
            if (info.getAppFirstReadTime() == null) {
                info.setAppFirstReadIp(SecurityUtils.getSubject().getSession().getHost());
                info.setAppFirstReadTime(new Date());
            } else {
                info.setAppLastReadIp(SecurityUtils.getSubject().getSession().getHost());
                info.setAppLastReadTime(new Date());
            }
        }

        if (StringUtils.isBlank(info.getId())) {
            info.setReportWeekId(id);
            info.setUserId(user.getId());
            oaReportWeekReadMapper.insert(info);
        } else {
            oaReportWeekReadMapper.updateById(info);
        }
    }
}
